//
//  HMSearchBar.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMSearchBar: UIView {

    @IBOutlet weak var textF: UITextField!
    @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var rightConstraints: NSLayoutConstraint!
    class func searchBar()->HMSearchBar{
        return NSBundle.mainBundle().loadNibNamed("SearchBarView", owner: nil, options: nil).last as! HMSearchBar
        
    }
    override func layoutSubviews() {
        //
        super.layoutSubviews()
        
    }
    override func awakeFromNib() {
        textF.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 40)
        textF.leftViewMode = .Always
        textF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        textF.leftView?.sizeToFit()
        textF.leftView?.contentMode = .Center
        textF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        
        self.bringSubviewToFront(textF)
        
        
    }
    @IBAction func textFBeginEditing(sender: UITextField) {
        rightConstraints.constant = self.btn.bounds.size.width
        UIView.animateWithDuration(0.25) { 
//            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    @IBAction func btnClick(sender: AnyObject) {
        rightConstraints.constant = 0
        UIView.animateWithDuration(0.25) {
            self.setNeedsLayout()
        }
        self.endEditing(true)
    }
}
